

#define test 1


/***************************************************************************/
/*                                                                         */
/* 234218 Data Structures 1, Winter 2008                                 */
/*                                                                         */
/* Homework : Wet 1					                                       */
/*                                                                         */
/***************************************************************************/

/***************************************************************************/
/*                                                                         */
/* File Name : main1.cpp                                                   */
/*                                                                         */
/* Holds the "int main()" function and the parser of the shell's           */
/* command line.                                                           */
/***************************************************************************/


#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include "library1.h"

#pragma warning(disable : 4996)



/* The command's strings */
static const char commandStr1[]			= "init";
static const char commandStr2[]	        = "newFan";
static const char commandStr3[]	        = "banFan";
static const char commandStr4[]		    = "getChair";
static const char commandStr5[]	        = "getNumMaccabiFans";
static const char commandStr6[]		    = "getNumHapoelFans";
static const char commandStr7[]		    = "getNumFreeChairs";
static const char commandStr8[]	        = "separateStands";
static const char commandStr11[]	    = "quit";


/* we assume maximum string size is not longer than 256  */
#define  MAX_STRING_INPUT_SIZE (255)
#define StrCmp(Src1,Src2) (0 == strncmp(Src1,Src2,strlen(Src1)) )


typedef enum {error_free, error} errorType;

static errorType parser(char *command);

#define ValidateRead(read_parameters,required_parameters,ErrorString) if(read_parameters!=required_parameters){printf(ErrorString);return error;}


bool is_init = false;
bool is_imp_bonus = false;

/***************************************************************************/
/* main			                                                           */
/***************************************************************************/
int main() {


#if test

	printf("If segmentation fault fix nullity check in library1.cpp.\n");

	int* nullPtr = NULL;
	int testPtr = NULL;

	newFan(NULL,0,0,0,0);
	banFan(NULL,0,0);
	getChair(NULL,0,0,0);
	getNumHapoelFans(NULL, 0, &testPtr);
	getNumHapoelFans(&testPtr, 0, NULL);
	getNumMaccabiFans(NULL,0, &testPtr);
	getNumMaccabiFans(&testPtr,0, NULL);
	getNumFreeChairs (NULL, 0, 0, &testPtr);
	getNumFreeChairs (&testPtr, 0, 0, NULL);
	separateStands (NULL, 0, 0);
	isImplementingBonus(NULL);
	quit(NULL);
	
	printf("Passed nullity check.\n");

	char buffer[MAX_STRING_INPUT_SIZE];
   
	// Reading commands
	while ( NULL != fgets(buffer, MAX_STRING_INPUT_SIZE, stdin) ) {
		fflush(stdout); 
		if ( error == parser(buffer) ) return 0;
	};
	return 0;

#else
//nothing here now
#endif
}







/***************************************************************************/
/* Parser                                                                  */
/***************************************************************************/

static errorType parser(char *command) { 
	
	static void *structure = NULL; /* The general data structure */
	
	char str[MAX_STRING_INPUT_SIZE];
	
	if ( NULL == command )         return error_free;
	if ( 0    == strlen(command) ) return error_free;
	if ( StrCmp("\n", command)   ) return error_free;
	
	// On init.	
	if ( StrCmp(commandStr1, command) ) {
		if( is_init )
		{
			printf("Init was already called.\n");
			return error_free;			
		}

		int S(0);
		ValidateRead( sscanf(command, "%s %d", str, &S), 2, "Init failed.\n" );
		structure = init(S);
		if( structure == NULL ) {
			printf("Init failed.\n");
			return error_free;
		}
		printf("Init succeeded.\n");

		// call for bonus checking is not necessary here
		// but will be used when checking your exercises
		// so make sure that you implement the function properly
		int res = isImplementingBonus(structure);
		if (res < 0 || res > 1) {
			printf("isImplementingBonus failed.\n");
			return error;
		}
		printf("isImplementingBonus is not relevant.\n");

		is_init = true;
		return error_free;
	}
	
	// new fan
	if ( StrCmp(commandStr2, command) ) { 
		int id, stand, chair, team;
		ValidateRead( sscanf(command, "%s %d %d %d %d", str, &id, &stand, &chair, &team), 5, "New fan failed.\n" );
		int res = newFan(structure, id, stand, chair, team);
		if ( res >= SUCCESS )
			printf("Added new fan %d in stand %d, chair %d, team %d\n", id, stand, chair, team);
		else
			printf("New fan failed with error %d.\n", res);
		return error_free;
	}
	
	// ban fan
	if ( StrCmp(commandStr3, command) ) { 
		int stand, chair;
		ValidateRead( sscanf(command, "%s %d %d", str, &stand, &chair), 3, "Ban fan failed.\n" );
         
		int res = banFan( structure, stand, chair);
		if ( res >= SUCCESS ) {
			printf("Banned fan\n");
		}
		else
			printf("Ban fan failed with error %d.\n", res);
		return error_free;
	}
	
	// get chair
	if ( StrCmp(commandStr4, command) ) { 
		int id, stand, chair;
		ValidateRead( sscanf(command, "%s %d", str, &id), 2, "Get chair failed.\n" );
         
		int res = getChair(structure, id, &stand, &chair);
		if ( res >= SUCCESS )
			printf("Fan %d sits in stand %d chair %d\n", id, stand, chair);
		else
			printf("Get chair failed with error %d.\n", res);
		return error_free;
	}
	
	// get Maccabi fans
	if ( StrCmp(commandStr5, command) ) { 
		int stand, numFans;
		ValidateRead( sscanf(command, "%s %d", str, &stand), 2, "Get Maccabi fans failed.\n" );
         
		int res = getNumMaccabiFans(structure, stand, &numFans );
		if (  res >= SUCCESS )
			printf("Number of Maccabi fans in stand %d is %d\n",  stand, numFans);
		else
			printf("Get Maccabi fans with error %d.\n", res);
		return error_free;
	}

	// get Hapoel fans
	if ( StrCmp(commandStr6, command) ) { 
		int stand, numFans;
		ValidateRead( sscanf(command, "%s %d", str, &stand), 2, "Get Hapoel fans failed.\n" );
         
		int res = getNumHapoelFans(structure, stand, &numFans );
		if (  res >= SUCCESS )
			printf("Number of Hapoel fans in stand %d is %d\n",  stand, numFans);
		else
			printf("Get Hapoel fans with error %d.\n", res);
		return error_free;
	}

	// get number of free chairs
	if ( StrCmp(commandStr7, command) ) { 
		int stand, chair, freeChairs;
		ValidateRead( sscanf(command, "%s %d %d", str, &stand, &chair), 3, "Get number of free chairs has failed.\n" );
         
		int res = getNumFreeChairs(structure, stand, chair, &freeChairs );
		if (  res >= SUCCESS )
			printf("Number of free chairs is %d\n",  freeChairs);
		else
			printf("Get number of free chairs has failed with error %d.\n", res);
		return error_free;
	}
	
	// separate stands
	if ( StrCmp(commandStr8, command) ) { 
		int stand1, stand2;
		ValidateRead( sscanf(command, "%s %d %d", str, &stand1, &stand2), 3, "Separate stands failed.\n" );
         
		int res = separateStands(structure, stand1, stand2 );
		if (  res >= SUCCESS )
			printf("Separate stands succeeded\n");
		else
			printf("Separate stands has failed with error %d.\n", res);
		return error_free;
	}
		
	// quit 
	if ( StrCmp(commandStr11, command) ) {  
		quit(structure);
		is_init = false;
		printf("Deleting data structure.\n");
		return error_free;
	}

	//documentation
	if (*command=='*') {  
		printf("   %s", command);
		return error_free;
	}
	

	
	// else
	printf( "Unknown command: %s\n", command );
	return error;
}
